#include "inc/comconfig.h"
#include "ui_comconfig.h"
COMconfig::COMconfig(QSerialPort *serialPort,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::COMconfig),
    serialPort(serialPort)
{
    ui->setupUi(this);
    initUI();
    initSerialPort();

}

COMconfig::~COMconfig()
{
    delete ui;
}

void COMconfig :: initUI()
{
    ui->cbdatabit->addItems({"5", "6", "7", "8"});
    ui->cbdatabit->setCurrentIndex(3); // 默认8位数据位
    ui->cbstopbit->addItems({"1", "1.5", "2"});
    ui->cbstopbit->setCurrentIndex(0); // 默认1位停止位
    ui->cbevetbit->addItems({"None", "Even", "Odd", "Space", "Mark"});
    ui->cbevetbit->setCurrentIndex(0); // 默认无校验
}

void COMconfig :: initSerialPort()
{
    ui->cbPort->clear();
    // 获取可用串口
    QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
    foreach(const QSerialPortInfo &port, ports) {
        QString displayText = port.portName();
        if (!port.description().isEmpty()) {
            displayText += " : " + port.description();
        }
        ui->cbPort->addItem(displayText, port.portName());
    }

    // 添加常用波特率
    ui->cbBaudrate->clear();
    ui->cbBaudrate->addItems({"2400","4800","9600", "19200", "38400", "57600", "115200"});
    ui->cbBaudrate->setCurrentText("115200");
}

void COMconfig::on_btnConnect_clicked()
{
    int slaveadd=0;
    if(serialPort->isOpen()) {
        serialPort->close();
        ui->btnConnect->setText("连接");
    } else {
        QString portName = ui->cbPort->currentData().toString();
        serialPort->setPortName(portName);
        serialPort->setBaudRate(ui->cbBaudrate->currentText().toInt());

        // 设置数据位
        switch(ui->cbdatabit->currentIndex()) {
            case 0: serialPort->setDataBits(QSerialPort::Data5); break;
            case 1: serialPort->setDataBits(QSerialPort::Data6); break;
            case 2: serialPort->setDataBits(QSerialPort::Data7); break;
            default: serialPort->setDataBits(QSerialPort::Data8); break;
        }

        // 设置停止位
        switch(ui->cbstopbit->currentIndex()) {
            case 0: serialPort->setStopBits(QSerialPort::OneStop); break;
            case 1: serialPort->setStopBits(QSerialPort::OneAndHalfStop); break;
            default: serialPort->setStopBits(QSerialPort::TwoStop); break;
        }

        // 设置校验位
        switch(ui->cbevetbit->currentIndex()) {
            case 0: serialPort->setParity(QSerialPort::NoParity); break;
            case 1: serialPort->setParity(QSerialPort::EvenParity); break;
            case 2: serialPort->setParity(QSerialPort::OddParity); break;
            case 3: serialPort->setParity(QSerialPort::SpaceParity); break;
            case 4: serialPort->setParity(QSerialPort::MarkParity); break;
        }
        slaveadd=ui->slaveadd->text().toInt();
        serialPort->setFlowControl(QSerialPort::NoFlowControl);

        if(serialPort->open(QIODevice::ReadWrite)) {
            ui->btnConnect->setText("断开");
            QString currentDevice = ui->device_type->currentText(); // 获取设备类型
            emit portConnected(currentDevice); // 发射信号并传递设备类
            emit portConnect();
        }
    }

}
